<?php


namespace app\admin\model\view;


use app\admin\model\user\UserModel;
use app\admin\model\UserWriterVerifyModel;
use app\logic\model\view\UserWriterVerifyUserView as UserWriterVerifyUser;
use think\facade\Db;

class UserWriterVerifyUserView extends UserWriterVerifyUser
{

    function getList($request,$field,$limit){
        $list=$this
            ->where(function ($q) use ($request){
                if(check_request($request,"nickname")){
                    $q->whereLike("user_nickname","%{$request['nickname']}%");
                }
                if(check_request($request,"mobile")){
                    $q->whereLike("user_mobile","%{$request['user_mobile']}%");
                }
                if(check_request($request,"true_name")){
                    $q->whereLike("true_name","%{$request['true_name']}%");
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[strtotime($request['start_time']),strtotime($request['end_time']." 23:59:59")]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",strtotime($request['start_time']));
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",strtotime($request['end_time']." 23:59:59"));
                }
            })
            ->field($field)
            ->paginate($limit);
        if(!$list->isEmpty()){
            return $list->toArray();
        }else{
            return false;
        }
    }
    function findById($id){
        $item=$this->where("id",$id)->find();
        return $item;
    }
    function verify($request=[],$item=[]){
        global $_admin;
        Db::startTrans();
        try {
            if($request['status']==2){
                $setUserData=array(
                    "card_id"=>$item['card_id'],
                    "true_name"=>$item['true_name'],
                    "is_writer"=>1,
                );
                $res=(new UserModel())->setUser($item['uid'],$setUserData);
            }
            $update=array(
                "status"=>$request['status'],
            );
            (new UserWriterVerifyModel())->where("id",$item['id'])->save($update);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
        return true;
    }
}